﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WmpRemoteServer
{
    public partial class Form2 : Form
    {
        private WindowsMediaPlayerController wmp;
        private Server server;
        public Form2()
        {
            InitializeComponent();
        }

        private void wmp_InfoEvent(string info)
        {
            server.sendString(info);

            //string[] test = info.Split(';');
            //if (test.Length >= 1)
            //{
            //    if (test[0].Equals("playlist"))
            //    {
            //    }
            //    else if (test.Length == 9)
            //    {
            //        Status.Text = test[0];
            //        Repeat.Checked = bool.Parse(test[1]);
            //        Shuffle.Checked = bool.Parse(test[2]);
            //        Mute.Checked = bool.Parse(test[3]);
            //        Volume.Value = int.Parse(test[4]);
            //        NowPlaying.Text = test[5];
            //        CurrentPos.Text = test[6];
            //        Length.Text = test[7];
            //        double duration = double.Parse(test[7]);
            //        double current = double.Parse(test[6]);
            //        progressBar1.Value = (int)((current / duration) * 100);
            //    }
            //}
        }

        public void server_commandEvent(string data)
        {
            wmp.Commands(data);
        }

        private void Mute_CheckedChanged(object sender, EventArgs e)
        {
            bool mute = Mute.Checked;
            wmp.Mute(mute);
        }

        private void Repeat_CheckedChanged(object sender, EventArgs e)
        {
            bool loop = Repeat.Checked;
            wmp.Loop(loop);
        }

        private void Shuffle_CheckedChanged(object sender, EventArgs e)
        {
            bool shuffle = Shuffle.Checked;
            wmp.Shuffle(shuffle);
        }

        private void Volume_ValueChanged(object sender, EventArgs e)
        {
            int volume = (int) Volume.Value;
            wmp.Volume(volume);
        }

        private void Previous_Click(object sender, EventArgs e)
        {
            wmp.Previous();
        }

        private void PlayPause_Click(object sender, EventArgs e)
        {
            wmp.PlayPause();
        }

        private void Next_Click(object sender, EventArgs e)
        {
            wmp.Next();
        }

        private void Stop_Click(object sender, EventArgs e)
        {
            wmp.Stop();
        }

        private void StartServer_Click(object sender, EventArgs e)
        {
            wmp = new WindowsMediaPlayerController();
            server = new Server();            
            server.StartServer(int.Parse(Port.Text));
            server.commandEvent += new Server.NewCommandsEvent(server_commandEvent);
            wmp.InfoEvent += new WindowsMediaPlayerController.WMPRemoteEvent(wmp_InfoEvent);
            Runing.Text = "Server Running";
        }

        private void StopServer_Click(object sender, EventArgs e)
        {
            wmp.InfoEvent -= new WindowsMediaPlayerController.WMPRemoteEvent(wmp_InfoEvent);
            server.commandEvent -= new Server.NewCommandsEvent(server_commandEvent);
            wmp.CloseWMP();
            server.CloseSockets();
            Runing.Text = "Not Running";
        }

        private void Runing_Click(object sender, EventArgs e)
        {

        }

        //public void Close()
        //{
        //    wmp.InfoEvent -= new WindowsMediaPlayerController.WMPRemoteEvent(wmp_InfoEvent);
        //    wmp.Close();
        //    server.commandEvent -= new Server.NewCommandsEvent(server_commandEvent);
        //    server.CloseSockets();
        //}
    }
}
